package auth

import com.nimbusds.jwt.SignedJWT
import org.springframework.stereotype.Component
import org.springframework.web.servlet.HandlerInterceptor

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@Component
class JwtDecodeInterceptor implements HandlerInterceptor {
    @Override
    boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String header = request.getHeader("Authorization");
        if (header) {
            String token = header.substring(7);//"bearer "
            Map<String, Object> sjwt = SignedJWT.parse(token).getPayload().toJSONObject();
            request.setAttribute(Constants.JWT_USER_REAL_NAME, sjwt.get(Constants.JWT_USER_REAL_NAME));
        }
        return true
    }
}